package com.gw.booking.service;


import com.gw.booking.common.Util;
import com.gw.booking.dto.backend.PrivilegeListDtoOut;
import com.gw.booking.entity.Privilege;
import com.gw.booking.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
public class PrivilegeService {
	
	@Autowired
	SessionFactory sessionFactory;
	
	/**
	 * 报得所有权限
	 */
	public List<PrivilegeListDtoOut> txGetPrivileges(User.Type type) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
		Session session = sessionFactory.getCurrentSession();
		
		List<Object> priveleges = session.createCriteria(Privilege.class)
				.add(Restrictions.eq("type", type.name()))
				.list();
		
		return Util.convertBeanList(priveleges, PrivilegeListDtoOut.class);
	}

	/**
	 * 根据id取没有的权限
	 */
	public List<Privilege> getUnachievedPrivilegeByIds(Session session, User.Type type, String[] ids) {
		if (ids == null || ids.length == 0) {
			ids = new String[] {"-1"};
		}
		
		List<Integer> idList = new ArrayList<Integer>();
		for (String id : ids) {
			idList.add(Integer.parseInt(id));
		}
		
		return session.createCriteria(Privilege.class).add(Restrictions.not(Restrictions.in("id", idList)))
				.add(Restrictions.eq("type", type.name()))
				.list();
	}
}
